package cn.com.blueInfo.bpm.person.repository.impl;

import cn.com.blueInfo.bpm.common.entity.BaseEntity;
import cn.com.blueInfo.bpm.common.enums.BaseDeleted;
import cn.com.blueInfo.bpm.common.repository.BaseRepository;
import cn.com.blueInfo.bpm.person.entity.OrganizeLevel;
import cn.com.blueInfo.bpm.person.enums.OrganizeLevelType;
import cn.com.blueInfo.bpm.person.mapper.OrganizeLevelMapper;
import cn.com.blueInfo.bpm.person.repository.OrganizeLevelRepository;
import cn.com.blueInfo.bpm.person.request.OrganizeLevelDTO;
import cn.com.blueInfo.core.entity.CurrentLoginUserInfo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* 组织级别表 数据服务实现类
* @author suxch
* @since 2025-09-01 19:28:09
*/
@Log4j2
@Service
public class OrganizeLevelRepositoryImpl
        extends ServiceImpl<OrganizeLevelMapper, OrganizeLevel>
        implements OrganizeLevelRepository, BaseRepository<OrganizeLevel, OrganizeLevelDTO.QueryDTO> {

    @Autowired
    private CurrentLoginUserInfo currentLoginUserInfo;

    /**
     * 提供获取 CurrentLoginUserInfo 的方法（由实现类提供，避免接口中注入）
     * @return CurrentLoginUserInfo
     */
    @Override
    public CurrentLoginUserInfo getCurrentLoginUserInfo() {
        return currentLoginUserInfo;
    }

    /**
    * 新增组织级别
    * @param organizeLevel 组织级别参数
    */
    @Override
    public Integer createOrganizeLevel(OrganizeLevel organizeLevel) {
        return sqlExecuteResult(save(organizeLevel));
    }

    /**
     * 批量新增组织级别
     * @param organizeLevelList 组织级别参数列表
     */
    @Override
    public Integer batchCreateOrganizeLevel(List<OrganizeLevel> organizeLevelList) {
        return sqlExecuteResult(saveBatch(organizeLevelList), organizeLevelList.size());
    }

    /**
    * 删除组织级别
    * @param organizeLevelId 组织级别ID
    */
    @Override
    public Integer deleteOrganizeLevel(Long organizeLevelId) {
        OrganizeLevel organizeLevel = new OrganizeLevel();
        organizeLevel.setId(organizeLevelId);
        organizeLevel.setDeleted(BaseDeleted.DELETED);
        return sqlExecuteResult(removeById(organizeLevel));
    }

    /**
     * 批量删除组织级别
     * @param organizeLevelIdList 组织级别ID列表
     */
    @Override
    public Integer batchDeleteOrganizeLevel(List<Long> organizeLevelIdList) {
        List<OrganizeLevel> organizeLevelList = new ArrayList<>();
        for (Long organizeLevelId : organizeLevelIdList) {
            OrganizeLevel organizeLevel = new OrganizeLevel();
            organizeLevel.setId(organizeLevelId);
            organizeLevel.setDeleted(BaseDeleted.DELETED);
            organizeLevelList.add(organizeLevel);
        }
        return sqlExecuteResult(removeBatchByIds(organizeLevelList), organizeLevelList.size());
    }

    /**
    * 更新部分组织级别
    * @param organizeLevel 组织级别参数
    */
    @Override
    public Integer updateOrganizeLevelPartial(OrganizeLevel organizeLevel) {
        validateUpdateId(organizeLevel.getId());
        LambdaUpdateWrapper<OrganizeLevel> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OrganizeLevel::getId, organizeLevel.getId());
        return sqlExecuteResult(update(updateWrapper));
    }

    /**
    * 更新全部组织级别
    * @param organizeLevel 组织级别参数
    */
    @Override
    public Integer updateOrganizeLevel(OrganizeLevel organizeLevel) {
        validateUpdateId(organizeLevel.getId());
        return sqlExecuteResult(updateById(organizeLevel));
    }

    /**
    * 查询一条组织级别
    * @param organizeLevelId 组织级别ID
    */
    @Override
    public OrganizeLevel getOrganizeLevelById(Long organizeLevelId) {
        LambdaQueryWrapper<OrganizeLevel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrganizeLevel::getId, organizeLevelId);
        return getOne(queryWrapper);
    }

    /**
    * 分页查询组织级别
    * @param organizeLevelQueryDTO 组织级别参数
    */
    @Override
    public Page<OrganizeLevel> getOrganizeLevelPage(OrganizeLevelDTO.QueryDTO organizeLevelQueryDTO) {
        Page<OrganizeLevel> organizeLevelPage = new Page<>(
                organizeLevelQueryDTO.getPage(), organizeLevelQueryDTO.getSize());
        LambdaQueryWrapper<OrganizeLevel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, organizeLevelQueryDTO);

        BaseDeleted deleted = organizeLevelQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeletePage(organizeLevelPage, queryWrapper);
        } else {
            return page(organizeLevelPage, queryWrapper);
        }
    }

    /**
     * 根据条件查询 组织级别 数量
     * @param organizeLevelQueryDTO 组织级别参数
     */
    @Override
    public Long getOrganizeLevelCount(OrganizeLevelDTO.QueryDTO organizeLevelQueryDTO) {
        LambdaQueryWrapper<OrganizeLevel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, organizeLevelQueryDTO);

        BaseDeleted deleted = organizeLevelQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteCount(queryWrapper);
        } else {
            return count(queryWrapper);
        }
    }

    /**
     * 根据条件查询 组织级别
     * @param organizeLevelQueryDTO 组织级别参数
     */
    @Override
    public List<OrganizeLevel> getOrganizeLevelList(OrganizeLevelDTO.QueryDTO organizeLevelQueryDTO) {
        LambdaQueryWrapper<OrganizeLevel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper(queryWrapper, organizeLevelQueryDTO);

        BaseDeleted deleted = organizeLevelQueryDTO.getDeleted();
        if (ObjectUtils.isNotEmpty(deleted) && deleted.equals(BaseDeleted.DELETED)) {
            return baseMapper.selectDeleteList(queryWrapper);
        } else {
            return list(queryWrapper);
        }
    }

    private void queryWrapper(LambdaQueryWrapper<OrganizeLevel> queryWrapper,
                              OrganizeLevelDTO.QueryDTO organizeLevelQueryDTO) {

        String levelCode = organizeLevelQueryDTO.getLevelCode();
        queryWrapper.eq(StringUtils.isNotBlank(levelCode), OrganizeLevel::getLevelCode, levelCode);

        String levelName = organizeLevelQueryDTO.getLevelName();
        queryWrapper.eq(StringUtils.isNotBlank(levelName), OrganizeLevel::getLevelName, levelName);

        OrganizeLevelType levelType = organizeLevelQueryDTO.getLevelType();
        queryWrapper.eq(ObjectUtils.isNotEmpty(levelType), OrganizeLevel::getLevelType, levelType);

        addQueryWrapper(queryWrapper, organizeLevelQueryDTO, getCommonColumn());

    }

    private Map<String, SFunction<OrganizeLevel, ?>> getCommonColumn() {
        return BaseEntity.getCommonColumn(
                OrganizeLevel::getOrderNumber,
                OrganizeLevel::getTenantId,
                OrganizeLevel::getApplicationId,
                OrganizeLevel::getStatus,
                OrganizeLevel::getDeleted,
                OrganizeLevel::getDeleteUserId,
                OrganizeLevel::getDeleteTime,
                OrganizeLevel::getRemark,
                OrganizeLevel::getCreateUserId,
                OrganizeLevel::getCreateTime,
                OrganizeLevel::getUpdateUserId,
                OrganizeLevel::getUpdateTime,
                OrganizeLevel::getExtend1,
                OrganizeLevel::getExtend2,
                OrganizeLevel::getExtend3);
    }

}
